#ifndef __PVMREFCOUNTER_H__
#define __PVMREFCOUNTER_H__

#include "pvm/pvmtypes.h"
#include "pvm/pvmdefs.h"

class APVMRefCounter
{
protected:
	APVMRefCounter()
	{
		refCount = 0;
	};
	pvm_int refCount;
public:
	virtual ~APVMRefCounter() {};
	PVM_FORCEINLINE pvm_int AddRef()
	{
		if(refCount>=0)
			return refCount++;
		else
			return 0;
	}

	PVM_FORCEINLINE pvm_int Release()
	{
		pvm_int newRefCount = 0;
		if(refCount>0)
		{
			refCount--;
			newRefCount = refCount;
		}
	
		if(refCount==0)
		{
			refCount = -1;
			DoDestroy();
		}
		return newRefCount;
	}

	PVM_FORCEINLINE pvm_int GetRefCount() {return refCount;};

	virtual void DoDestroy() = 0;
};

#endif
